<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 传入数据 -->
        <tree-tools :tree-node="company" :is-root="true" />
        <!-- 树形组件 -->
        <!-- :data用来绑定数据 -->
        <!-- :props用来重定义字段 -->
        <!-- :default-expand-all是否默认展开所有节点 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <tree-tools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>
    </div>
    <!-- v-model一个组件只能用一次 -->
    <!-- v-model = :value + @input -->
    <!-- 一个组件可以有多个属性，每个属性都可以使用sync修饰符 -->
    <!-- attr.sync = :attr + @update:attr -->
    <!-- sync修饰符相当于在传入值的前提下又传入了自定义事件 -->
    <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import AddDept from './components/add-dept.vue'
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils/index.js'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      showDialog: false,
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company.name = result.companyName
      // 转换树形结构
      this.departs = transListToTreeData(result.depts, '')
    },
    addDepts(node) {
      // 显示弹层
      this.showDialog = true
      this.node = node
    },
    editDepts(node) {
      // 显示弹层
      this.showDialog = true
      this.node = node
      // 父组件 控制 add-dept组件
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

  <style scoped>
.tree-card {
  padding: 30px 140px;
  font-size:14px;
}
  </style>

